﻿using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Entity;
using System.Data.EntityClient;
using kysec.RAL.EFToMySql;


namespace kysec.Bll.Company
{
    /// <summary>
    /// 对所有静态页面的查询显示
    /// </summary>
    public class Search
    {
        /// <summary>
        /// 查找具体的静态信息根据类型
        /// </summary>
        /// <param name="type">输入静态信息的类别</param>
        /// <returns>返回找到静态信息的list</returns>
        public static List<tb_aboutme> getAllInfoByType(int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
                .RetrieveObject(string.Format("GetAllInfoByType_{0}"
                 , type));

            if (o == null)
            {                 
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();
                try
                {
                    var list = ctx.tb_aboutme.Where(c => c.c_i_typeId == type && c.c_b_isDelete == false).ToList();

                    if (list.Count != 0)
                    {
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                          .AddObjectWithAbsoluteTime(string.Format("GetAllInfoByType_{0}", type)
                          , list,
                          1
                          , System.Web.Caching.CacheItemPriority.Normal);
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    string error=ex.ToString()+ "\t查询aboutMe信息时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search),error);
                }
                finally {
                    ctx.Dispose();
                } return new List<tb_aboutme>();
            }
            return (List<tb_aboutme>)o;
            }

        /// <summary>
        /// 查找信息根据Id
        /// </summary>
        /// <param name="type">此类信息的类型</param>
        /// <param name="id">此类信息的Id</param>
        /// <returns>返回找到的结果集</returns>
        public static List<tb_aboutme> getAllInfoById(int type,string id)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
                .RetrieveObject(string.Format("GetAllInfoById_{0}_{1}"
                 , type,id));

            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();

                try
                {
                    Guid amId = Guid.Parse(kysec.CR.Common.EncodeByBase64.Decode(id));
                    id = amId.ToString();
                    var list = ctx.tb_aboutme.Where(c => c.c_i_typeId == type && c.c_b_isDelete == false && c.c_uq_amId .Equals(id)).ToList();

                    if (list.Count != 0)
                    {
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                          .AddObjectWithAbsoluteTime(string.Format("GetAllInfoById_{0}_{1}", type,id)
                          , list,
                          1
                          , System.Web.Caching.CacheItemPriority.Normal);
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t查询aboutMe信息时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search), error);
                }
                finally
                {
                    ctx.Dispose();
                } return new List<tb_aboutme>();
            }
            return (List<tb_aboutme>)o;
        }

        /// <summary>
        /// 查找信息根据Id
        /// </summary>
        /// <param name="type">此类信息的类型</param>
        /// <param name="id">此类信息的Id</param>
        /// <returns>返回找到的结果集</returns>
        public static List<tb_aboutme> getAllInfoForpre(int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity() // 从Cache中读取信息
                .RetrieveObject(string.Format("GetAllInfoForpre_{0}"
                 , type));

            if (o == null)
            {
                MySql_kysecContectionString ctx = new MySql_kysecContectionString();

                try
                {
                    var list = ctx.tb_aboutme.Where(c => c.c_i_typeId == type && c.c_b_isDelete == false && c.c_b_isUsing == true).ToList();

                    if (list.Count != 0)
                    {
                        kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity()
                          .AddObjectWithAbsoluteTime(string.Format("GetAllInfoForpre_{0}", type)
                          , list,
                          10000
                          , System.Web.Caching.CacheItemPriority.Normal);
                        return list;
                    }
                }
                catch (Exception ex)
                {
                    string error = ex.ToString() + "\t前台查询aboutMe信息时出错";
                    kysec.CR.Logger.MyLogger.Error(typeof(Search), error);
                }
                finally
                {
                    ctx.Dispose();
                } return new List<tb_aboutme>();
            }
            return (List<tb_aboutme>)o;
        }        
    }

    /// <summary>
    /// 对静态信息的操作类
    /// </summary>
    public class aboutme
    {
        /// <summary>
        /// 插入静态信息，返回成功信息
        /// </summary>
        /// <param name="model">数据集</param>
        /// <returns>0：添加失败 1：添加成功 2：添加异常</returns>
        public static string AddAboutmeInfo(tb_aboutme model)
        {
            string result = "0";

            Guid id = Guid.Parse(kysec.CR.Common.EncodeByBase64.Decode(HttpContext.Current.Session["userId"].ToString()));
            MySql_kysecContectionString ctx = new MySql_kysecContectionString();

            try
            {
                tb_aboutme aboutMe=new tb_aboutme{ 
                    c_uq_userId = id.ToString(),
                    c_uq_amId = Guid.NewGuid().ToString(),
                    c_i_typeId = model.c_i_typeId,
                    c_ntx_content = model.c_ntx_content,
                    c_dt_publishDate = DateTime.Now,
                    c_b_isUsing = true,
                    c_b_isDelete = false
                };

                ctx.AddObject("tb_aboutme", aboutMe);
                result = ctx.SaveChanges().ToString();
            }
            catch (Exception ex)
            {
                string error = ex.ToString() + "\t插入信息时出错";
                kysec.CR.Logger.MyLogger.Error(typeof(aboutme), error);
                result = "2";
            }
            finally { ctx.Dispose(); }
            return result;
        }

        /// <summary>
        /// 修改静态信息
        /// </summary>
        /// <param name="newsId">需要修改的静态的id</param>
        /// <param name="model">数据集</param>
        /// <returns>0：信息不存在或修改失败 1：修改成功 2：修改异常</returns>
        public static string UpdateAboutmeInfo(string id, tb_aboutme model)
        {
            string result = "0";

            MySql_kysecContectionString ctx = new MySql_kysecContectionString();

            try
            {
                Guid amId = Guid.Parse(kysec.CR.Common.EncodeByBase64.Decode(id));
                id = amId.ToString();
                var list = ctx.tb_aboutme.Where(c => c.c_uq_amId.Equals(id) && c.c_b_isDelete == false).ToList();

                if (list.Count == 0)
                    result = "0";
                else
                {
                    foreach (var item in list)
                    {
                        item.c_ntx_content = model.c_ntx_content;
                    }
                    result = ctx.SaveChanges().ToString();
                }
            }
            catch (Exception ex)
            {
                string error = ex.ToString() + "\t修改信息时出错";
                kysec.CR.Logger.MyLogger.Error(typeof(aboutme), error);
                result = "2";
            }
            finally { ctx.Dispose(); }
            return result;
        }

        /// <summary>
        /// 删除静态信息，可以同时恢复删除的新闻
        /// </summary>
        /// <param name="newsId">需要删除的新闻信息的Id</param>
        /// <returns>0：没有找到信息或删除失败 1：删除成功 2：删除异常</returns>
        public static string DeleteAboutmeInfo(string id)
        {
            string result = "0";

            MySql_kysecContectionString ctx = new MySql_kysecContectionString();

            try
            {
                Guid amId = Guid.Parse(kysec.CR.Common.EncodeByBase64.Decode(id));
                id = amId.ToString();
                var list = ctx.tb_aboutme.Where(c => c.c_uq_amId.Equals(id)).ToList();

                if (list.Count == 0)
                    result = "0";
                else
                {
                    foreach (var item in list)
                    {
                        if (item.c_b_isDelete)
                            item.c_b_isDelete = false;
                        else
                            item.c_b_isDelete = true;
                    }

                    result = ctx.SaveChanges().ToString();
                }
            }
            catch (Exception ex)
            {
                string error = ex.ToString() + "\t删除信息时出错";
                kysec.CR.Logger.MyLogger.Error(typeof(aboutme), error);
                result = "2";
            }
            finally { ctx.Dispose(); }
            return result;
        }

        /// <summary>
        /// 设置信息是否正在使用
        /// </summary>
        /// <param name="amId">需要设置的信息Id</param>
        /// <returns>0：没有找到信息或者操作失败 1：更改成功 2：更改异常</returns>
        public static string SetTypeInfo(string id)
        {
            string result = "0";

            MySql_kysecContectionString ctx = new MySql_kysecContectionString();

            try
            {
                Guid amId = Guid.Parse(kysec.CR.Common.EncodeByBase64.Decode(id));
                id = amId.ToString();
                var list = ctx.tb_aboutme.Where(c => c.c_uq_amId.Equals(id) && c.c_b_isDelete == false).ToList();

                if (list.Count == 0)
                    result = "0";
                else
                {
                    foreach (var item in list)
                    {
                        if (item.c_b_isUsing )
                            item.c_b_isUsing = false;
                        else
                            item.c_b_isUsing = true;
                    }
                    result = ctx.SaveChanges().ToString();
                }
            }
            catch (Exception ex)
            {
                string error = ex.ToString() + "\t设置新闻类型信息时出错";
                kysec.CR.Logger.MyLogger.Error(typeof(aboutme), error);
                result = "2";
            }
            finally { ctx.Dispose(); }
            return result;
        }

    }
}
